package com.ggh.smartreply;

import android.annotation.TargetApi;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.content.DialogInterface;
import android.app.AlertDialog;

public class ShowDataBaseActivity extends Activity{

    private SQLiteDatabase db;
    private Cursor cursor;
    SimpleCursorAdapter sca;
    ListView lv;
    TextView tv;







    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.showdatabaseactivity);

        RuleDataBaseHelper ruleDataBaseHelper = new RuleDataBaseHelper(ShowDataBaseActivity.this, "text_RULE");
        tv = (TextView) findViewById(R.id.TextView01);
        lv = (ListView) findViewById(R.id.ListView01);
        db = ruleDataBaseHelper.getReadableDatabase();
        cursor = db.query("RULE", new String[]{"_id", "_check", "_reply"}, null, null, null, null, null);
        if(cursor.getCount()>0){
            tv.setText("如果要删除某条规则，请单击该规则");
        }
        sca=new SimpleCursorAdapter(ShowDataBaseActivity.this, R.layout.item, cursor, new String[]{"_check", "_reply"}, new int[]{R.id.TextView02,R.id.TextView03},1);
        lv.setAdapter(sca);
        db.close();

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int which, long arg3) {

                new AlertDialog.Builder(ShowDataBaseActivity.this).setTitle("删除规则").setMessage("确定要删除该规则吗？").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        RuleDataBaseHelper ruleDataBaseHelper = new RuleDataBaseHelper(ShowDataBaseActivity.this, "text_RULE");
                        db = ruleDataBaseHelper.getWritableDatabase();
                        db.delete("RULE", "_id=" + cursor.getInt(cursor.getColumnIndex("_id")), null);

                        cursor = db.query("RULE", new String[]{"_id", "_check", "_reply"}, null, null, null, null, null);
                        sca=new SimpleCursorAdapter(ShowDataBaseActivity.this, R.layout.item, cursor, new String[]{"_check", "_reply"}, new int[]{R.id.TextView02,R.id.TextView03},1);
                        lv.setAdapter(sca);
                        db.close();
                    }
                }).show();
            }
        });
    }
}
